page.js ➔ ... ➔ cmsData.source.catch   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 1
1
import path from 'path'
2
import {
3
  cmsData,
4
  config,
5
  Page,
6
  cmsTemplates,
7
  coreUtils
8
} from '../../cli'
9
10
var page = function (req, res) {
11
  var html = (req.query.html) ? true : false
12
  var json = null
13
  var editor = false
14
  if(typeof req.body.json !== 'undefined' && req.body.json !== null) {
15
    editor = true
16
    if(typeof req.body.json === 'string') {
17
      json = JSON.parse(req.body.json)
18
    }else {
19
      json = req.body.json
20
    }
21
  }
22
23
  var filepath = req.originalUrl.replace('/abe/page', '')
24
  
25
  if(typeof filepath !== 'undefined' && filepath !== null) {
26
    var jsonPath = null
27
    var linkPath = null
28
29
    var filePathTest = cmsData.revision.getDocumentRevision(filepath)
30
    if(typeof filePathTest !== 'undefined' && filePathTest !== null) {
31
      jsonPath = filePathTest.path
32
      linkPath = filePathTest.abe_meta.link
33
    }
34
35
    if(jsonPath === null || !coreUtils.file.exist(jsonPath)) { 
36
      res.status(404).send('Not found')
37
      return
38
    } 
39
40
    if(typeof json === 'undefined' || json === null) {
41
      json = cmsData.file.get(jsonPath)
42
    }
43
    
44
    let meta = config.meta.name
45
46
    var templateId = ''
47
    if(json[meta] && json[meta].template) {
48
      templateId = json[meta].template
49
    }else {
50
      templateId = req.params[0]
51
    }
52
    var text = cmsTemplates.template.getTemplate(templateId, json)
53
54
    if (!editor) {
55
56
      cmsData.source.getDataList(path.dirname(linkPath), text, json)
57
        .then(() => {
58
          var page = new Page(templateId, text, json, html)
59
          res.set('Content-Type', 'text/html')
60
          res.send(page.html)
61
        }).catch(function(e) {
62
          console.error(e)
63
        })
64
    }else {
65
      text = cmsData.source.removeDataList(text)
66
      var page = new Page(templateId, text, json, html)
67
      res.set('Content-Type', 'text/html')
68
      res.send(page.html)
69
    }
70
  }else {
71
    // not 404 page if tag abe image upload into each block
72
    if(/upload%20image/g.test(req.url)) {
73
      var b64str = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
74
      var img = new Buffer(b64str, 'base64')
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
75
76
      res.writeHead(200, {
77
        'Content-Type': 'image/jpeg',
78
        'Content-Length': img.length
79
      })
80
      res.end(img) 
81
    }else {
82
      res.status(404).send('Not found')
83
    }
84
  }
85
}
86
87
export default page